<?php

namespace App\Logic\finger;

use App\Logic\Code;
use App\Models\MinerInfo;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;


/**
 * @author AdamTyn
 * @description <用户>数据模型
 */
class UserBalanceLogic
{


    //添加
    public static function transfer($data,$user,$scene){

        $validateRule = [
            'amount' => 'required',
            'code' => 'required',
            'token' => 'required',
        ];
        $errorMsg = [
            'amount.required' => '请传入划转数量',
            'code.required' => '请传入验证码',
            'token.required' => '请传入币种信息 FIL',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }


        $key = $user['phone'] . '_code_' . $scene;
        $code_info = Code::code($key, $data['code']);
        if ($code_info['status'] === 'ERROR') {
//            return $code_info;
        }

        $rel_amount = DB::table('user_balance')
            ->where('token', $data['token'])
            ->where('user_name', $user['username'])
            ->sum('amount');

        if ($rel_amount < $data['amount']) {
//            return e_msg(300026, '划转余额不足');
        }

        $balance_insert_arr = [
            'token' => $data['token'],
            'user_name' => $user['username'],
            'amount' => '-'.$data['amount'],
            'ticket' => 'data_transfer_' . date('YmdHis'),
            'type' => 'transfer',
            'type_note' => '转出',
        ];

        $fil_insert_arr = [
            'out_account' => $user['username'],
            'out_phone' => $user['phone'],
            'shift_account' => '自己',
            'shift_phone' => '自己',
            'amount' => $data['amount'],
            'type_note' => '矿机资金划转',
            'type' => 'user_transfer',
        ];

        //用户表添加fil，用户钱包表记录，fil表记录
        DB::beginTransaction();

        $rel1 = DB::table('user')->where('id',$user['id'])->increment('fil',$data['amount']);
        $rel2 = DB::table('user_balance')->insert($balance_insert_arr);
        $rel3 = DB::table('record_fil')->insert($fil_insert_arr);
        if($rel1&&$rel2&&$rel3){
            DB::commit();
            return s_msg('');
        }else{
            DB::rollBack();
            return e_msg(123456,'有问题');
        }


    }


    //系统修改
    public static function sys_transfer($data){

        $validateRule = [
            'id' => 'required',
            'amount' => 'required',
            'token' => 'required',
        ];
        $errorMsg = [
            'id.required' => '请传入用户id id',
            'amount.required' => '请传入划转数量 amount',
            'token.required' => '请传入币种信息 FIL',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }


        $user = DB::table('user')->where('id',$data['id'])->select('username','phone')->first();
        if(!$user){
            return e_msg(123456,'该用户不存在');
        }

        if($data['amount'] > 0){
            $type = 'sys_shift';
        }else{
            $type = 'sys_out';
        }

        $fil_insert_arr = [
            'out_account' => $user->username,
            'out_phone' => $user->phone,
            'shift_account' => '系统',
            'shift_phone' => '系统',
            'amount' => $data['amount'],
            'type_note' => '系统修改',
            'type' => $type,
        ];


        //用户表添加fil，用户钱包表记录，fil表记录
        DB::beginTransaction();
        $rel1 = DB::table('user')->where('id',$data['id'])->increment('fil',$data['amount']);
        $rel2 = DB::table('record_fil')->insert($fil_insert_arr);
        if($rel1&&$rel2){
            DB::commit();
            return s_msg('');
        }else{
            DB::rollBack();
            return e_msg(123456,'有问题');
        }


    }



}
